package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.patchmanage.back.entity.Menu;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-04-14
 */
@Api(value = " 菜单 controller", tags = {"菜单 Api"})
@RestController
@RequestMapping("/back/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /**
     * 获取菜单列表
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取菜单列表接口", notes = "获取菜单列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pId", value = "pId", required = false, dataType = "Integer", paramType = "body")
    })
    public Result<JSONArray> list(@RequestBody Dict dict) {
        Integer pId = dict.getInt("pId");
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<Menu>();
        if(pId != null){
            queryWrapper.eq("p_id",pId);
        }
        List<Menu> groupList = menuService.list(queryWrapper);
        return Result.success(treeMenuList(JSONUtil.parseArray(groupList),0));
    }

    private JSONArray treeMenuList(JSONArray menuList, int parentId) {
        JSONArray childMenu = new JSONArray();
        for (Object object : menuList) {
            JSONObject jsonMenu = JSONUtil.parseObj(object);
            int menuId = jsonMenu.getInt("id");
            int pid = jsonMenu.getInt("pId");
            if (parentId == pid) {
                JSONArray c_node = treeMenuList(menuList, menuId);
                jsonMenu.putOnce("childNode", c_node);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

    @ApiOperation(value = "新增菜单列表接口", notes = "新增菜单列表接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public Result<Boolean> saveOrUpdate(@RequestBody Menu menu){
        return Result.success(menuService.saveOrUpdate(menu));
    }

    @ApiOperation(value = "删除菜单接口", notes = "删除菜单接口")
    @RequestMapping(value = "removeByIds", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids", required = true, dataType = "String", paramType = "body")
    })
    public Result<Boolean> removeByIds(@RequestBody Dict dict){
        String id = dict.getStr("ids");
        String[] ids = id.split(",");
        return Result.success(menuService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation(value = "获取菜单接口", notes = "获取菜单接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "body")
    })
    public Result<Menu> getById(@RequestBody Dict dict){
        String id = dict.getStr("id");
        return Result.success(menuService.getById(id));
    }

}
